// Call the dataTables jQuery plugin
$(document).ready(function () {
    $('#dataTable').DataTable();
    $('#dataTable1').DataTable();
});


// 添加机型
$("#addAircraftModelBtn").click(function () {
    var tInput = $("#addAircraftModelName");
if (tInput.val()) {
    $.ajax({
        url: ctxPath + "admin/addAircraftModel",
        type: 'post',
        data: {"modelName": tInput.val()},
        dataType: 'json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg("添加成功", 'alert-success');
                location.reload();
            }
        }
    });
} else {
    showMsg("名称不可为空", 'alert-warning');
}
});

// 删除机型
function deleteAircraftModel(modelID) {
    $.ajax({
        url: ctxPath + "admin/deleteAircraftModel/" + modelID,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg("删除成功", 'alert-success');
                location.reload();
            }
        }
    });
}

// 修改机型
function modifyAircraftModel(modelID, btn) {
    modifyName = $("#modifyAircraftModelName" + modelID).val();
    console.log(modifyName);
    if (typeof (modifyName) == "undefined") {
        var tmpEl = $("#aircraftModelName" + modelID);
        var t = tmpEl.text();
        tmpEl.empty();
        tmpEl.append("<div class='row'>" +
            "           <input id='modifyAircraftModelName" + modelID + "' type='text' class='col-8 form-control form-text' placeholder='名称' value='" + t + "'/>" +
            "           <span class='col-1'></span>" +
            "           <span  class='col-2'>" +
            "              <button onclick='location.reload()' class='btn-sm btn-info'><i class='fas fa-times'></i></button>\n" +
            "           </span></div>");
        $('i', $(btn)).attr("class", "fas fa-check");
    } else {
        if (modifyName) {
            $.ajax({
                url: ctxPath + "admin/updateAircraftModel",
                type: 'post',
                data: {"modelID": modelID, "modelName": modifyName},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("修改成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        } else {
            showMsg("名称不可为空", 'alert-warning');
        }
    }
}

// 添加航线
$("#addFlightBtn").click(function () {
    var flightNo = $("#addFlightNo").val();
    var modelID = $("#addModelID").val();
    var startTerminalID = $("#addFlightstartID").val();
    var endTerminalID = $("#addFlightendID").val();
    var ticketPricing = $("#addFlightPrice").val();
    if (flightNo && ticketPricing) {
        $.ajax({
            url: ctxPath + "admin/addFlight",
            type: 'post',
            data: {"flightNo": flightNo,
                    "startTerminalID": startTerminalID,
                    "endTerminalID": endTerminalID,
                    "modelID": modelID,
                    "ticketPricing":ticketPricing},
            dataType: 'json',
            success: function (data) {
                if (data["result"] == 1) {
                    showMsg("添加成功", 'alert-success');
                    location.reload();
                }
            }
        });
    } else {
        showMsg("录入项不可为空", 'alert-warning');
    }
})

// 删除航线
function deleteFlight(flightID) {
    $.ajax({
        url: ctxPath + "admin/deleteFlight/" + flightID,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg("删除成功", 'alert-success');
                location.reload();
            }
        }
    });
}

// 修改航线
function modifyFlight(flightID,modelID,startID,endID,btn) {
    var modifyFlightNo = $("#modifyFlightNo"+flightID).val();
    if (typeof (modifyFlightNo) == "undefined") {
        var flightNoEl = $("#flightNo"+flightID);
        var flightNo = flightNoEl.text();
        flightNoEl.empty();
        flightNoEl.append("<input id='modifyFlightNo"+flightID+"' type='text' class='form-control' value='"+flightNo+"' placeholder='航班号'>");

        var modelNameEl = $("#modelName"+flightID);
        modelNameEl.empty();
        modelNameEl.append("<select id='modifyModelID"+flightID+"' class='form-control'>"+$("#addModelID").html()+"</select>");
        $("#modifyModelID"+flightID).val(modelID);

        var flightStartEl = $("#flightStart"+flightID);
        flightStartEl.empty();
        flightStartEl.append("<select id='modifyFlightstartID"+flightID+"' class='form-control'>"+$("#addFlightstartID").html()+"</select>");
        $("#modifyFlightstartID"+flightID).val(startID);

        var flightEndEl = $("#flightEnd"+flightID);
        flightEndEl.empty();
        flightEndEl.append("<select id='modifyFlightendID"+flightID+"' class='form-control'>"+$("#addFlightendID").html()+"</select>");
        $("#modifyFlightendID"+flightID).val(endID);

        var ticketPricingEl = $("#ticketPricing"+flightID);
        var ticketPricing = ticketPricingEl.text();
        ticketPricingEl.empty();
        ticketPricingEl.append("<input id='modifyFlightPrice"+flightID+"' type='number' min='0.00' step='0.01' class='form-control' value='"+ticketPricing+"' placeholder='基础价格'>");

        $('i', $(btn)).attr("class", "fas fa-check");
    }else {
        var modifyModelID = $("#modifyModelID"+flightID).val();
        var modifyStartTerminalID = $("#modifyFlightstartID"+flightID).val();
        var modifyEndTerminalID = $("#modifyFlightendID"+flightID).val();
        var modifyTicketPricing = $("#modifyFlightPrice"+flightID).val();
        if (modifyFlightNo&&modifyTicketPricing) {
            $.ajax({
                url: ctxPath + "admin/updateFlight",
                type: 'post',
                data: {"flightID":flightID,
                        "flightNo": modifyFlightNo,
                        "startTerminalID": modifyStartTerminalID,
                        "endTerminalID": modifyEndTerminalID,
                        "modelID": modifyModelID,
                        "ticketPricing":modifyTicketPricing},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("修改成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        } else {
            showMsg("修改项不可为空", 'alert-warning');
        }
    }
}

// 显示提示框
function showMsg(msg, style, time) {
    style = (style === undefined) ? 'alert-success' : style;
    time = (time === undefined) ? 1200 : time;
    $('.alert').html(msg).addClass(style).show().delay(time).fadeOut();
}